package com.ruoyi.project.WebMoudle.wxHelp.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.WebMoudle.ShopSuser.domain.ShopSuser;
import com.ruoyi.project.WebMoudle.ShopSuser.service.IShopSuserService;
import com.ruoyi.project.WxMoudle.WebSocket.WebSocketServer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.wxHelp.domain.WxHelp;
import com.ruoyi.project.WebMoudle.wxHelp.service.IWxHelpService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 帮助与反馈Controller
 *
 * @author ruoyi
 * @date 2020-07-31
 */
@Controller
@RequestMapping("/wxHelp/wxHelp")
public class WxHelpController extends BaseController {
    private String prefix = "wxHelp/wxHelp";

    @Autowired
    private IWxHelpService wxHelpService;

    @Autowired
    private WebSocketServer webSocketServer;


    @RequiresPermissions("wxHelp:wxHelp:view")
    @GetMapping()
    public String wxHelp() {
        return prefix + "/wxHelp";
    }


    @GetMapping("/shopHelp")
    public String shopHelp(ModelMap mmap) {
        WxHelp wxHelp = new WxHelp();
        wxHelp.setQuestionType("3");
        List<WxHelp> wxHelps = wxHelpService.selectWxHelpList(wxHelp);
        mmap.put("wxHelps", wxHelps);
        return prefix + "/shopHelp";
    }

    @RequiresPermissions("wxHelp:wxHelp:edit")
    @GetMapping("/toRemark/{id}")
    public String toRemark(@PathVariable("id") Long id, ModelMap mmap) {
        WxHelp wxHelp = wxHelpService.selectWxHelpById(id);
        mmap.put("wxHelp", wxHelp);
        return prefix + "/remark";
    }

    /**
     * 已处理页面
     *
     * @return
     */
    @RequiresPermissions("wxHelp:wxHelp:view")
    @GetMapping("/finishHelp")
    public String finishHelp() {
        return prefix + "/finishHelp";
    }

    /**
     * 处理详情页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        WxHelp wxHelp = wxHelpService.selectWxHelpById(id);
        mmap.put("wxHelp", wxHelp);
        return prefix + "/detail";
    }

    /*未处理页面*/
    @RequiresPermissions("wxHelp:wxHelp:view")
    @GetMapping("/unDoneHelp")
    public String userHelp() {
        return prefix + "/unDoneHelp";
    }

    /*查询未处理数量*/
    @PostMapping("/queryHelpMessage")
    @ResponseBody
    public Object queryHelpMessage() {
        int i = wxHelpService.queryHelpMessage();
        return i;

    }

    /*未处理数据*/
    @RequiresPermissions("wxHelp:wxHelp:list")
    @PostMapping("/unDoneHelpList")
    @ResponseBody
    public TableDataInfo unDoneHelpList(WxHelp wxHelp) {
        startPage();
        List<WxHelp> list = wxHelpService.unDoneHelpList(wxHelp);
        return getDataTable(list);
    }

    /*已处理数据*/
    @RequiresPermissions("wxHelp:wxHelp:list")
    @PostMapping("/doneHelpList")
    @ResponseBody
    public TableDataInfo doneHelpList(WxHelp wxHelp) {
        startPage();
        List<WxHelp> list = wxHelpService.doneHelpList(wxHelp);
        return getDataTable(list);
    }

    /**
     * 查询帮助与反馈列表
     */
    @RequiresPermissions("wxHelp:wxHelp:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxHelp wxHelp) {
        startPage();
        List<WxHelp> list = wxHelpService.selectWxHelpList(wxHelp);
        return getDataTable(list);
    }

    /**
     * 查询常见问题展示
     */
    @RequiresPermissions("wxHelp:wxHelp:list")
    @PostMapping("/selectSysList")
    @ResponseBody
    public TableDataInfo selectSysList(WxHelp wxHelp) {
        startPage();
        List<WxHelp> list = wxHelpService.selectSysList(wxHelp);
        return getDataTable(list);
    }

    /**
     * 导出帮助与反馈列表
     */
    @RequiresPermissions("wxHelp:wxHelp:export")
    @Log(title = "帮助与反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxHelp wxHelp) {
        List<WxHelp> list = wxHelpService.selectWxHelpList(wxHelp);
        ExcelUtil<WxHelp> util = new ExcelUtil<WxHelp>(WxHelp.class);
        return util.exportExcel(list, "wxHelp");
    }

    /**
     * 新增帮助与反馈
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存帮助与反馈
     */
    @RequiresPermissions("wxHelp:wxHelp:add")
    @Log(title = "帮助与反馈", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxHelp wxHelp) {
        return toAjax(wxHelpService.insertWxHelp(wxHelp));
    }

    @PostMapping("/addShopQuestion")
    @ResponseBody
    public AjaxResult addShopQuestion(WxHelp wxHelp) {

        wxHelp.setWxUserId(getUserId());  // 商铺id
        wxHelp.setQuestionType("2");    //商家提问
        int i = wxHelpService.insertWxHelp(wxHelp);
        if (i>0){
            webSocketServer.sendAll("发送消息");
        }
        return toAjax(i);
    }

    /**
     * 修改帮助与反馈
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        WxHelp wxHelp = wxHelpService.selectWxHelpById(id);
        mmap.put("wxHelp", wxHelp);
        return prefix + "/edit";
    }

    /**
     * 修改保存帮助与反馈
     */
    @RequiresPermissions("wxHelp:wxHelp:edit")
    @Log(title = "帮助与反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxHelp wxHelp) {
        if (wxHelp.getQuestionType().equals("1") || wxHelp.getQuestionType().equals("2")) {
            wxHelp.setStatus("1");
        }
        int i = wxHelpService.updateWxHelp(wxHelp);
        webSocketServer.sendAll("发送消息");
        return toAjax(i);
    }

    /**
     * 删除帮助与反馈
     */
    @RequiresPermissions("wxHelp:wxHelp:remove")
    @Log(title = "帮助与反馈", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wxHelpService.deleteWxHelpByIds(ids));
    }
}
